class BinaryTree {
  constructor() {
    // 二叉树根节点
    this.root = null
  }

  // 实例调用的插入节点方法
  insert (key) {
    let newNode = new Node(key)
    if (this.root === null) {
      this.root = newNode
    } else {
      insertNode(this.root, newNode)
    }
  }
}

// 生成二叉树节点
const Node = function (key) {
  this.key = key
  this.left = null
  this.right = null
}

// 插入节点
const insertNode = function (node, newNode) {
  if (newNode.key < node.key) {
    if (node.left === null) {
      node.left = newNode
    } else {
      insertNode(node.left, newNode)
    }
  } else {
    if (node.right === null) {
      node.right = newNode
    } else {
      insertNode(node.right, newNode)
    }
  }
}

export default BinaryTree
